package me.yushuai.wukong.base.controller;

import me.yushuai.wukong.base.api.*;
import me.yushuai.wukong.base.api.dto.CreateZoneDTO;
import me.yushuai.wukong.base.api.dto.CreateZoneGroupDTO;
import me.yushuai.wukong.base.api.dto.*;
import me.yushuai.wukong.base.api.vo.ZoneGroupVO;
import me.yushuai.wukong.base.api.vo.ZoneVO;
import me.yushuai.wukong.base.convertor.ZoneConvertor;
import me.yushuai.wukong.base.service.ZoneGroupService;
import me.yushuai.wukong.base.service.ZoneService;
import me.yushuai.wukong.common.dto.PageDTO;
import me.yushuai.wukong.common.error.WuKongWebException;
import me.yushuai.wukong.common.exceptin.BizIdNotExistException;
import me.yushuai.wukong.common.exceptin.DuplicateBizIdException;
import me.yushuai.wukong.common.exceptin.NotExistException;
import me.yushuai.wukong.common.mvc.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 库区控制器
 *
 * @author zhoushuai@189.cn
 * @since 2022-08-25
 */
@RestController
public class WarehouseZoneController extends BaseController implements WarehouseZoneApi {

    /**
     * 库区业务类
     */
    @Autowired
    private ZoneService zoneService;

    @Autowired
    private ZoneConvertor zoneConvertor;

    /**
     * 区域业务类
     */
    @Autowired
    private ZoneGroupService zoneGroupService;

    @Override
    public Long createZone(CreateZoneDTO dto) throws WuKongWebException {
        try {
            return this.zoneService.createZone(dto);
        } catch (DuplicateBizIdException e) {
            final Object[] params = {e.getBizId()};
            throw new WuKongWebException(
                    super.messageSource.getMessage("Zone.bizId.HasExist", params,
                            "The bizId of Zone \"" + e.getBizId() + "\" is has exits", getCurrentLocale())
            );
        } catch (NotExistException e) {
            String code = e.getTypeName() + "id.NotFound";
            String defaultMessage = "The id of " + e.getTypeName() + " is not found";
            final Object[] params = {e.getId()};
            throw new WuKongWebException(super.messageSource.getMessage(code, params, defaultMessage, super.getCurrentLocale()));
        }
    }

    @Override
    public Long editZone(Long zoneId, UpdateZoneDTO dto) throws WuKongWebException {
        dto.setZoneId(zoneId);
        try {
            return this.zoneService.updateZone(dto);
        } catch (NotExistException e) {
            String code = e.getTypeName() + "id.NotFound";
            String defaultMessage = "The id of " + e.getTypeName() + " is not found";
            final Object[] params = {e.getId()};
            throw new WuKongWebException(super.messageSource.getMessage(code, params, defaultMessage, super.getCurrentLocale()));
        }
    }

    @Override
    public Long deleteZone(Long zoneId) throws WuKongWebException {
        try {
            this.zoneService.deleteZone(zoneId);
            return zoneId;
        } catch (NotExistException e) {
            String code = e.getTypeName() + "id.NotFound";
            String defaultMessage = "The id of " + e.getTypeName() + " is not found";
            final Object[] params = {e.getId()};
            throw new WuKongWebException(super.messageSource.getMessage(code, params, defaultMessage, super.getCurrentLocale()));
        }
    }

    @Override
    public ZoneVO getZoneById(Long zoneId) throws WuKongWebException {
        try {
            ZoneDTO zone = this.zoneService.findZoneById(zoneId);
            return this.zoneConvertor.mapToZoneVo(zone);
        } catch (NotExistException e) {
            String code = e.getTypeName() + "id.NotFound";
            String defaultMessage = "The id of " + e.getTypeName() + " is not found";
            final Object[] params = {e.getId()};
            throw new WuKongWebException(super.messageSource.getMessage(code, params, defaultMessage, super.getCurrentLocale()));
        }
    }

    @Override
    public PageDTO<ZoneVO> pagingSearchZone(PagingSearchZone dto) {
        PageDTO<ZoneDTO> zonePage = this.zoneService.pagingSearch(dto);
        List<ZoneVO> zoneList = this.zoneConvertor.mapToZoneVoList(zonePage.getData());
        return PageDTO.of(dto, zoneList);
    }


    @Override
    public Long createZoneGroup(CreateZoneGroupDTO dto) throws WuKongWebException {
        try {
            return this.zoneGroupService.createZoneGroup(dto);
        } catch (DuplicateBizIdException e) {
            final Object[] params = {e.getBizId()};
            throw new WuKongWebException(
                    super.messageSource.getMessage("ZoneGroup.bizId.HasExist", params,
                            "The bizId of ZoneGroup \"" + e.getBizId() + "\" is has exits", getCurrentLocale())
            );
        }
    }

    @Override
    public Long editZoneGroup(Long zoneGroupId, UpdateZoneGroupDTO updateZoneGroupDTO) throws WuKongWebException {
        try {
            updateZoneGroupDTO.setZoneGroupId(zoneGroupId);
            return this.zoneGroupService.updateZoneGroup(updateZoneGroupDTO);
        } catch (BizIdNotExistException e) {
            final Object[] params = {e.getBizId()};
            throw new WuKongWebException(
                    super.messageSource.getMessage("ZoneGroup.bizId.NotFound", params,
                            "The bizId of ZoneGroup \"" + e.getBizId() + "\" is not found.", getCurrentLocale())
            );
        }
    }

    @Override
    public Long deleteZoneGroup(Long id) throws WuKongWebException {
        try {
            this.zoneGroupService.deleteZoneGroup(id);
        } catch (NotExistException e) {
            final Object[] params = {e.getId()};
            String code = e.getTypeName() + ".bizId.NotFound";
            String defaultMessage = "The bizId of ZoneGroup \"" + e.getId() + "\" is not found.";
            throw new WuKongWebException(super.messageSource.getMessage(code, params, defaultMessage, getCurrentLocale()));
        }
        return id;
    }

    @Override
    public ZoneGroupVO getZoneGroupById(Long id) throws WuKongWebException {
        try {
            ZoneGroupDTO zoneGroup = this.zoneGroupService.findZoneGroupById(id);
            return this.zoneConvertor.mapToZoneGroupVo(zoneGroup);
        } catch (NotExistException e) {
            final Object[] params = {e.getId()};
            String code = e.getTypeName() + ".bizId.NotFound";
            String defaultMessage = "The bizId of ZoneGroup \"" + e.getId() + "\" is not found.";
            throw new WuKongWebException(super.messageSource.getMessage(code, params, defaultMessage, getCurrentLocale()));
        }
    }

    @Override
    public PageDTO<ZoneGroupVO> pagingSearchZoneGroup(PagingSearchZoneGroup dto) {
        PageDTO<ZoneGroupDTO> zoneGroupPage = this.zoneGroupService.pagingSearchZoneGroup(dto);
        List<ZoneGroupVO> zoneGroupList = this.zoneConvertor.mapToZoneGroupVoList(zoneGroupPage.getData());
        return PageDTO.of(dto, zoneGroupList);
    }
}
